//
//  QYGLoginViewController.m
//  wawaji_ios
//
//  Created by 云+物娱 on 2017/12/11.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGLoginViewController.h"
#import "QYGNetRequestModel.h"
#import <WXApi.h>
#import "QYGNetRequestModel.h"
#import "QYGConfigModel.h"
#import "QYGUserInfoModel.h"
#import "QYGTabbarController.h"
#import "QYGAccountInputView.h"

@interface QYGLoginViewController ()

@property (nonatomic, strong) UIButton *loginBtn;
@property (nonatomic, strong) UIButton *detailBtn;
@property (nonatomic, strong) QYGAccountInputView *accountInputView;
@end

@implementation QYGLoginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"login_bg.png" ofType:nil];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:imagePath]];
    imageView.userInteractionEnabled = YES;
    self.view = imageView;
    
    [self.view addSubview:self.detailBtn];
    if([QYGKeychain isShowPay]){
        [self.view addSubview:self.loginBtn];
    }else{
        [self.view addSubview:self.accountInputView];
        WS(weakSelf);
        [self.accountInputView setLoginBlock:^(NSString *userName, NSString *paswd) {
            [weakSelf accountLoginWithUserName:userName password:paswd];
        }];
    }
    if([QYGKeychain token]){
        self.view.userInteractionEnabled = NO;
        [QYGNetRequestModel getUserInfoCompleted:^(QYGUserInfoModel *userInfo) {
            if(userInfo.avatar || userInfo.nickname){
                [self goToMainWithUserInfo:userInfo];
            }
            self.view.userInteractionEnabled = YES;
        } WithErrorCodeBlock:^(NSError *error) {
            self.view.userInteractionEnabled = YES;
        }];
    }

    
    WS(weakSelf);
    [[SGUtil shareUtil] setWxLoginCompleteBlock:^(NSString *code) {
        
        weakSelf.view.userInteractionEnabled = NO;
        [QYGNetRequestModel getWechatUserInfoWithCode:code completed:^(QYGUserInfoModel *userInfo){
             [weakSelf goToMainWithUserInfo:userInfo];
             [QYGKeychain setToken:userInfo.api_token];
             weakSelf.view.userInteractionEnabled = YES;
         } WithErrorCodeBlock:^(NSError *error) {
             weakSelf.view.userInteractionEnabled = YES;
         }];
    }];
    
}
- (void)goToMainWithUserInfo:(QYGUserInfoModel *)userInfo{
    
    QYGTabbarController *tabbarVC = [[QYGTabbarController alloc] init];
    tabbarVC.userInfo = userInfo;
    KeyWindow.rootViewController = tabbarVC;
}
- (void)accountLoginWithUserName:(NSString *)username password:(NSString *)password{
    
    self.view.userInteractionEnabled = NO;
    [QYGNetRequestModel postLoginWithAccountUserName:username password:password  Completed:^(QYGUserInfoModel *userInfo) {
        
        [self goToMainWithUserInfo:userInfo];
        [QYGKeychain setToken:userInfo.api_token];
        self.view.userInteractionEnabled = YES;
    } WithErrorCodeBlock:^(NSError *error) {
        self.view.userInteractionEnabled = YES;
    }];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    self.detailBtn.centerX = self.view.centerX;
    self.detailBtn.y = self.view.height - self.detailBtn.height - SafeBottom - ScaleW(35);
    self.loginBtn.centerX = self.view.centerX;
    self.loginBtn.y = self.detailBtn.y - ScaleW(45) - self.loginBtn.height;
    self.accountInputView.centerX = self.view.centerX;
    self.accountInputView.y = self.detailBtn.y - self.accountInputView.height;
}

- (void)wechatLoginClick{
    QYLog(@"登录");
    if( [SGUtil WXAppIsInstalled]){
        SendAuthReq *req = [[SendAuthReq alloc] init];
        req.scope = @"snsapi_userinfo";
        req.state = @"QYGwawajiApp";
        [WXApi sendReq:req];
    }
}

- (UIButton *)loginBtn{
    if(!_loginBtn){
        _loginBtn = [UIButton buttonWithTitle:@"微信登录" setTitleColor:[UIColor whiteColor] setTitleFont:ScaleFont(18) setState:UIControlStateNormal];
        [_loginBtn viewWithcornerRadius:10 borderWidth:0 borderColor:nil backgroundColor:[UIColor colorWithHex:@"09bb07"]];
        _loginBtn.size = CGSizeMake(ScaleW(167), ScaleW(42));
        [_loginBtn addTarget:self action:@selector(wechatLoginClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}

- (UIButton *)detailBtn{
    if(!_detailBtn){
        _detailBtn = [UIButton buttonWithTitle:@"—— 云+物娱在线抓娃娃机一站式解决方案提供商 ——" setTitleColor:[UIColor colorWithHex:@"999999"] setTitleFont:ScaleFont(12) setState:UIControlStateNormal];
        _detailBtn.userInteractionEnabled = NO;
        _detailBtn.size = CGSizeMake(SCREEN_WIDTH, ScaleW(13));
    }
    
    return _detailBtn;
}

- (QYGAccountInputView *)accountInputView{
    if(!_accountInputView){
        _accountInputView = [[QYGAccountInputView  alloc] init];
        _accountInputView.size = CGSizeMake(SCREEN_WIDTH - ScaleW(60), ScaleW(100));
    }
    return _accountInputView;
}
@end
